import { NextResponse } from 'next/server'

// 简单的百度链接主动推送接口
// 使用方式：POST /api/baidu-push，body 为要推送的 URL 数组
// 需在部署环境配置 BAIDU_PUSH_TOKEN
export async function POST(request: Request) {
  try {
    const token = process.env.BAIDU_PUSH_TOKEN
    if (!token) {
      return NextResponse.json({ ok: false, error: 'Missing BAIDU_PUSH_TOKEN' }, { status: 500 })
    }

    const { urls } = await request.json()
    if (!Array.isArray(urls) || urls.length === 0) {
      return NextResponse.json({ ok: false, error: 'urls must be a non-empty array' }, { status: 400 })
    }

    const endpoint = `http://data.zz.baidu.com/urls?site=https://www.xsbj.cn&token=${token}`
    const resp = await fetch(endpoint, {
      method: 'POST',
      headers: { 'Content-Type': 'text/plain' },
      body: urls.join('\n'),
    })

    const text = await resp.text()
    return new NextResponse(text, {
      status: resp.status,
      headers: { 'Content-Type': 'application/json; charset=utf-8' },
    })
  } catch (e: any) {
    return NextResponse.json({ ok: false, error: e?.message || 'unknown error' }, { status: 500 })
  }
}
